package com.example.service;

import com.example.dao.MerchantDao;
import com.example.entity.Merchant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
@Transactional
public class MerchantServiceImpl implements MerchantService {

      private MerchantDao merchantDao;

      @Autowired
      public MerchantServiceImpl(MerchantDao merchantDao) {
            this.merchantDao = merchantDao;
      }

      @Override
      public Merchant findMerchantByID(Integer randomID) {
            return merchantDao.findMerchantByID(randomID);
      }

      @Override
      public void register(Merchant merchant) {
            merchantDao.register(merchant);
      }

      @Override
      public Merchant login(String merchantName, String merchantPwd,Integer id) {

            Merchant merchant= merchantDao.findMerchantByName(merchantName,merchantPwd,id);
            if (ObjectUtils.isEmpty(merchant))throw new RuntimeException("商家不存在");
//          比较密码
            if (!merchant.getMerchantPwd().equals(merchantPwd))  throw new RuntimeException("密码错误");

            return merchant;
      }

      @Override
      public void updateMerchantByID(Merchant merchant) {
            merchantDao.updateMerchantByID(merchant);
      }

      @Override
      public void updateRegister(Integer id) {
            merchantDao.updateRegister(id);
      }
}
